package com.itheima.comsys.demo;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class CompetitionManager {
    private List<Competition> competitions = new ArrayList<>();
    private List<Participant> participants = new ArrayList<>();

    // 添加竞赛方法
    public boolean addCompetition(Competition competition) {
        // 检查赛事发布者发布的竞赛是否有时间限制（此处假设赛事发布者发布的竞赛没有时间限制）
        if (competition.getPublisher() != null) {
            competitions.add(competition);
            return true;
        }
        // 普通竞赛检查时间冲突
        for (Competition existingCompetition : competitions) {
            if (existingCompetition.getStartTime().isBefore(competition.getEndTime()) && existingCompetition.getEndTime().isAfter(competition.getStartTime())) {
                return false;
            }
        }
        competitions.add(competition);
        return true;
    }

    // 删除竞赛方法
    public void deleteCompetition(String competitionName) {
        competitions.removeIf(competition -> competition.getName().equals(competitionName));
        participants.removeIf(participant -> participant.getCompetitionName().equals(competitionName));
    }

    // 获取所有竞赛方法
    public List<Competition> getAllCompetitions() {
        return competitions;
    }

    // 报名竞赛方法
    public boolean registerParticipant(Participant participant) {
        Competition competition = getCompetitionByName(participant.getCompetitionName());
        if (competition != null) {
            participants.add(participant);
            return true;
        }
        return false;
    }

    // 根据竞赛名称获取竞赛对象方法
    private Competition getCompetitionByName(String competitionName) {
        for (Competition competition : competitions) {
            if (competition.getName().equals(competitionName)) {
                return competition;
            }
        }
        return null;
    }
}